
// Definition for singly-linked list.
function ListNode(val) {
    this.val = val;
    this.next = null;
}


/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var detectCycle = function(head) {
  let slow = head, fast = head;
  while(fast != null && fast.next != null) {
    slow = slow.next;
    fast = fast.next.next;
    if(slow == fast) break;
  }
  if(fast == null || fast.next == null) return null;
  fast = head;
  while(fast != slow) {
    slow = slow.next;
    fast = fast.next;
  }
  return fast;
};
let head = [3,2,0,-4], pos = 1;
console.log(detectCycle(head));
